www.gusucode.com > 24Beta 虚拟主机版 1.0.0 Beta源码程序 > 24Beta 虚拟主机版 1.0.0 Beta源码程序/24Beta-1.0.0-vhost/protected/modules/admin/controllers/UserController.php

    <?php

class UserController extends CController
{
	public function actionList()
	{
	    $criteria = new CDbCriteria();
	    $criteria->limit = 20;
	    
	    $pages = new CPagination(User::model()->count($criteria));
		$pages->pageSize = 20;
		$pages->applyLimit($criteria);
		
		$sort = new CSort('User');
        $sort->defaultOrder = 'id asc';
        $sort->applyOrder($criteria);
		
		
	    $users = User::model()->findAll($criteria);
		$this->render('list', array(
			'users'=> $users,
			'pages'=> $pages,
		    'sort' => $sort,
		));
	}


	public function actionDelete()
	{
	    if (app()->request->isAjaxRequest && app()->request->isPostRequest) {
			$uid = isset($_GET['uid']) ? $_GET['uid'] : null;
			$uids = isset($_POST['chkuser']) ? $_POST['chkuser'] : null;
			if (!empty($uid) && is_numeric($uid)) {
    			$result = User::model()->deleteByPk($uid);
    			$data['result'] = $result ? 1 : 0;
    			$data['message'] = '删除' . ($result ? '成功' : '失败');
			} elseif (count($uids) > 0) {
				$strids = '(`' . implode('`, `', $uids) . '`)';
			    $result = User::model()->deleteAll("`id` in $strids");
			    $data['result'] = $result ? 1 : 0;
    			$data['message'] = '删除' . ($result ? '成功' : '失败');
			} else {
			    $data['result'] = -1;
    			$data['message'] = '参数格式错误';
			}
		}
		else {
			$data['result'] = -1;
    		$data['message'] = '非法请求';
		}
		echo json_encode($data);
	}
	
	public function actionCreate()
	{
	    if (app()->request->isPostRequest) {
	        unset($_POST['submit']);
	        $name = trim($_POST['name']);
	        if (empty($name)) {
	            $prompt['result'] = false;
	            $prompt['note'][] = array('text' => '用户名不能为空');
	            $prompt['note'][] = array('text' => '重新添加用户', 'url'=>url('admin/user/create'));
	            $prompt['note'][] = array('text' => '返回重试', 'url'=>'javascript:history.back();');
	            $this->render('create', array('prompt'=>$prompt));
	            exit(0);
	        }
	        $u = User::model()->findByAttributes(array('name' => $name));
	        
	        if ($u !== null) {
	            $prompt['result'] = false;
                $prompt['note'][] = array('text' => $name . '&nbsp;用户已经存在');
	            $prompt['note'][] = array('text' => '添加新的用户', 'url'=>url('admin/user/create'));
	        } else {
	            $user = new User($_POST);
	            $result = $user->save();
	            if ($result) {
	                $prompt['result'] = true;
                    $prompt['note'][] = array('text' => $name . '&nbsp;用户添加成功');
	                $prompt['note'][] = array('text' => '继续添加用户', 'url'=>url('admin/user/create'));
	                $prompt['note'][] = array('text' => '查看用户列表', 'url'=>url('admin/user/list'));
	            } else {
    	            $prompt['result'] = false;
                    $prompt['note'][] = array('text' => $name . '&nbsp;用户添加失败');
	                $prompt['note'][] = array('text' => '重新添加用户', 'url'=>url('admin/user/create'));
	                $prompt['note'][] = array('text' => '返回重试', 'url'=>'javascript:history.back();');
    	        }
	        }
	    }
	    $this->render('create', array('prompt'=>$prompt));
	}
	
	
	// -----------------------------------------------------------
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}